/**
 * Copyright (C), 2015-2018, 真格邦软件科技有限公司
 * 文件名: WorkFlowApi
 * 作者: lsyzx【892366102@qq.com】
 * 创建时间: 2018/12/26 15:27
 * 描述: TODO
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.zgb.manager.api.sys;

import com.alibaba.dubbo.config.annotation.Reference;
import com.zx.sys.api.SysApiService;
import com.zx.sys.entity.ZgbTable;
import com.zx.utils.generator.GeneratorUtil;
import com.zx.utils.request.sys.DbTablesRequest;
import com.zx.utils.request.sys.GeneratorRequest;
import com.zx.utils.vo.ApiResult;
import com.zx.utils.vo.ResultEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 作者: lsyzx【892366102@qq.com】
 * 创建时间: 2018/12/26 15:27
 * 描述: 系统服务OpenApi
 * 历史修改:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
@RestController
@RequestMapping(value = "/api/sys", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(description = "系统相关API")
public class SysApi {

    @Reference(version = "1.0.0")
    private SysApiService sysApiService;


    /**
     * 生成mybatis配置
     *
     * @param generatorRequest
     * @return
     */
    @ApiOperation(value = "生成mybatis配置", notes = "生成mybatis配置")
    @RequestMapping(value = "/generator", method = RequestMethod.GET)
    public ApiResult generator(GeneratorRequest generatorRequest) {
        try {
            GeneratorUtil.genCode(generatorRequest.getPath(), generatorRequest.getIp(), generatorRequest.getDb(), generatorRequest.getPort(), generatorRequest.getUsername(), generatorRequest.getPassword(), generatorRequest.getModelpackagename(), generatorRequest.getDaopackagename(), generatorRequest.getMapperpath(), generatorRequest.getTablename(), generatorRequest.getTablemodel());
            return new ApiResult(ResultEnum.SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(ResultEnum.FAIL, e);
        }
    }

    /**
     * 查询数据库表
     *
     * @param dbTablesRequest
     * @return
     */
    @ApiOperation(value = "查询数据库表", notes = "查询数据库表")
    @RequestMapping(value = "/query_db_tables", method = RequestMethod.GET)
    public ApiResult query_db_tables(DbTablesRequest dbTablesRequest) {
        try {
            List<ZgbTable> list = sysApiService.getZgbTables(dbTablesRequest.getDb_url(), dbTablesRequest.getUser_name(), dbTablesRequest.getPassword(), dbTablesRequest.getTable_schema());
            return new ApiResult(ResultEnum.SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new ApiResult(ResultEnum.FAIL, e);
        }
    }

}
